﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Text;

namespace HQWeb.SysManage.ProjectManage
{
    public partial class ProjectEdit : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                VerdictIsLogin();
                if (!Page.IsPostBack)
                {
                    this.txtSign.Text = Request["sign"];

                    if (this.txtSign.Text == "0")  //添加
                    {
                        this.txtPIdHid.Text = Request["pid"];
                        this.labTitle.Text = "添加项目分类信息";

                    }
                    bindList();
                    if (this.txtSign.Text.Trim() == "1")  //编辑
                    {
                        this.txtIdHid.Text = Request["id"];
                        this.labTitle.Text = "修改项目分类信息";
                        fillForm();
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        #region 绑定项目下拉框
        private void bindList()
        {

            BLL.SysManage.T_PUB_PROJECTTYPE ProjectBll = new BLL.SysManage.T_PUB_PROJECTTYPE();
            IList il = ProjectBll.GetModelList("");
            PID.ClearSelection();
            PID.DataSource = il;
            PID.DataTextField = "PROJECTTYPE_NAME"; //dropdownlist的Text的字段 
            PID.DataValueField = "PROJECTTYPE_ID";//dropdownlist的Value的字段 
            PID.DataBind();
            ListItem item = new ListItem("无上级", "0");
            PID.Items.Add(item);
            if (this.txtSign.Text.Trim() == "0")
            {
                PID.SelectedValue = this.txtPIdHid.Text;

            }
        }
        #endregion

        #region 填充页面数据
        protected void fillForm()
        {
            BLL.SysManage.T_PUB_PROJECTTYPE ProjectBll = new BLL.SysManage.T_PUB_PROJECTTYPE();
            Model.SysManage.T_PUB_PROJECTTYPE ProjectModel = new Model.SysManage.T_PUB_PROJECTTYPE();
            int temp;
            int.TryParse(this.txtIdHid.Text, out temp);
            ProjectModel = ProjectBll.GetModel(temp);

            this.TRADE_NAME.Text = ProjectModel.PROJECTTYPE_NAME;
            this.PID.SelectedValue = ProjectModel.PARENT_ID.ToString();
            this.PROJECT_RADIO.Checked = ProjectModel.IS_NULL;
            this.txtPIdHid.Text = ProjectModel.PARENT_ID.ToString();
        }
        #endregion

        #region 提交按钮事件
        protected void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                BLL.SysManage.T_PUB_PROJECTTYPE ProjectBll = new BLL.SysManage.T_PUB_PROJECTTYPE();
                Model.SysManage.T_PUB_PROJECTTYPE ProjectModel = new Model.SysManage.T_PUB_PROJECTTYPE();
                ProjectModel.PROJECTTYPE_ID = Convert.ToInt32(string.IsNullOrEmpty(this.txtIdHid.Text.Trim().ToString()) ? "-1" : this.txtIdHid.Text.Trim().ToString());
                int pID = Convert.ToInt32(this.PID.SelectedValue.Trim());
                string strPath;
                ProjectModel.PARENT_ID = pID;
                ProjectModel.PROJECTTYPE_NAME = this.TRADE_NAME.Text.Trim();
                ProjectModel.IS_NULL = this.PROJECT_RADIO.Checked;
                Model.SysManage.T_PUB_PROJECTTYPE ProjecPartModel = ProjectBll.GetModel(pID);
                if (ProjecPartModel!=null)
                {
                    strPath = ProjecPartModel.IDPATH;
                }
                else
                {
                    strPath = "0/";
                }
                int intID;
                if (this.txtSign.Text.Trim() == "0")
                {

                   intID= ProjectBll.Add(ProjectModel);
                   ProjectModel.PROJECTTYPE_ID = intID;
                   ProjectModel.IDPATH = strPath + intID.ToString() + "/";
                   ProjectBll.Update(ProjectModel);
                    Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "添加" + ProjectModel.PROJECTTYPE_NAME + "项目", DateTime.Now);
                }
                else if (this.txtSign.Text.Trim() == "1")
                {
                    intID = ProjectModel.PROJECTTYPE_ID;
                    ProjectModel.IDPATH = strPath + intID.ToString() + "/";
                    ProjectBll.Update(ProjectModel);
                    Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "更新" + ProjectModel.PROJECTTYPE_NAME + "项目信息", DateTime.Now);
                }
                HQWeb.View.GetSearchIndex.strHtml = "";
                StringBuilder strl = new StringBuilder("<script language='javascript'>\n");
                strl.Append("window.parent.location.reload();\n");
                strl.Append("location.href =\"ProjectList.aspx?pid=" + this.PID.SelectedValue + "\";\n");
                strl.Append("</script>\n");
                HttpContext.Current.Response.Write(strl.ToString());
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
                //COM.HS.Utility.JScript.Alert(ex.Message);
                //return;
            }
            //catch
            //{
            //    throw;
            //}
        }
        #endregion
    }
}